package graduate.cslg.com.universityshopping;

import android.content.Intent;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import graduate.cslg.com.universityshopping.ZhiHubean.News;
import graduate.cslg.com.universityshopping.adapter.NewsSummaryAdapter;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import graduate.cslg.com.universityshopping.utils.HtmlFormat;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ContentActivity extends AppCompatActivity {

    private CollapsingToolbarLayout mCollapsingToolbarLayout;
    private ImageView newsImage;
    private WebView newsContent;
    private Toolbar mToolbar;
    private int id;
    private News mNews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);

        initViews();
        Intent intent = getIntent();
        id = intent.getIntExtra(NewsSummaryAdapter.NEWS_ID, 0);
        getNewsInfo(id);
    }

    private void initViews() {
        mCollapsingToolbarLayout = findViewById(R.id.toolbar_layout);
        newsImage = findViewById(R.id.news_image);
        newsContent = findViewById(R.id.news_content);
        mToolbar = findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    private void getNewsInfo(int id) {
        RetrofitDevelop.getZhiHuService()
                .getNews(id)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<News>() {
                    @Override
                    public void onCompleted() {
                        Glide.with(ContentActivity.this).load(mNews.getImage()).into(newsImage);
                        newsContent.loadDataWithBaseURL(null, HtmlFormat.getNewContent(mNews.getBody()), "text/html", "utf-8", null);
                        newsContent.getSettings().setJavaScriptEnabled(true);
                        mCollapsingToolbarLayout.setTitle(mNews.getTitle());
                        newsContent.setWebChromeClient(new WebChromeClient());
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(News news) {
                        mNews = news;
                    }
                });
    }
}
